@extends('layouts.back_stage')
@section('style')
    @parent
    <link rel="stylesheet" href="{{env('CDN_DOMAIN_NAME')}}/css/dateSelect.css"/>
@endsection
@section('script')
    @parent
    <script src="{{env('CDN_DOMAIN_NAME')}}/js/dateSelect.js"></script>
@endsection
@section('content')
    <style>
        /*.table-hover th,.table-hover td{text-align: center;}*/
        .order_img {
            width: 50px;
            height: 100%;
            float: left;
            margin-right: 10px;
        }

        .order_img img {
            width: 100%;
            padding: 30px 0;
        }

        .order_ul {
            margin-bottom: 0;
            float: left;
        }

        .order_li {
            text-align: start;
        }

        .order_p {
            margin-bottom: 0;
            text-align: start;
        }

        .form-control {
            font-size: 12px;
        }
    </style>
    <div class="container">
        <div style="padding:20px 20px 20px 0;overflow:hidden;"><h3 style="float:left;">普通订单列表</h3></div>
        <div>
            <form action="/order" role="form" style="display: inline-block;">
                @if($self_company_id == 1)
                    <label>选择公司：</label>
                    <select name="company_id" id="sort" class="address com_id" style="width: 150px">
                        @if(isset($_GET['company_id']) && $_GET['company_id'])
                            <option value="{{$_GET['company_id']}}"
                                    selected>{{ $company_name[$_GET['company_id']]}}</option>
                        @endif
                        <option value="">全部公司</option>
                        @foreach($company_name as $k=>$v)
                            <option value="{{ $k }}">{{ $v }}</option>
                        @endforeach
                    </select>
                @else
                    <input type="hidden" class="com_id" name="company_id" value="{{ $self_company_id }}">
                @endif
                <label>订单状态：</label>
                <select name="status" id="sort" class="address statu_id" style="width: 150px">
                    @if(isset($_GET['status']) && $_GET['status'] != '')
                        <option value="{{$_GET['status']}}">{{ $status[$_GET['status']] }}</option>
                    @endif
                    <option value="">全部</option>
                    @foreach($status as $k=>$v)
                        <option value="{{ $k }}">{{ $v }}</option>
                    @endforeach
                </select>
                <label>用户名：</label><input type="text" placeholder="请输入用户名" class="form-control user_name"
                                          style="width: 150px;display: inline-block;" value="{{ $user_name }}"
                                          name="user_name">
                <label>手机号：</label><input type="text" placeholder="请输入手机号" class="form-control tel_id"
                                          style="width: 150px;display: inline-block;" value="{{ $tel }}"
                                          name="user_tel">
                <label>款号：</label><input type="text" placeholder="请输入款号" class="form-control style_id"
                                         style="width:150px;display: inline-block;" value="{{ $girard }}" name="girard">
                <label>时间：</label><input id="date1" class="form-control start_time"
                                         style="width:120px;display: inline-block;background:#fff;" name="start_at"
                                         type="text" value="{{ $start_at }}" readonly placeholder="开始时间"/>
                <input id="date2" class="form-control end_time" name="end_at"
                       style="width:120px;display: inline-block;background:#fff;" type="text" value="{{ $end_at }}"
                       readonly placeholder="结束时间"/>
                <button class="btn btn-info" style="margin-bottom:3px;">搜索</button>
            </form>
            <a class="btn btn-primary" style="float: right;" href="javascript:;" onclick="Export()">导出</a>
        </div>
        @if(count($colorInfos)>0)
            <div class="form-group"
                 style="padding:15px 0;border-bottom:1px solid #e7eaec;margin-bottom:0;float:left;width:100%;">
                @foreach($colorInfos as $k=>$v)
                    <ul style="margin-bottom:0;margin-right:20px;float:left;">
                        <li>颜色：{{ $k }}</li>
                        <li>数量：{{ $v }}</li>
                    </ul>
                @endforeach
            </div>
        @endif
        <span style="display:none;" class="info_sum">{{$orders_count}}</span>
        <table class="table table-hover" style="margin-top:20px;">
            <thead>
            <tr>
                <th class="col-sm-2">订单信息</th>
                <th>商户订单号</th>
                <th style="width:190px;">用户信息</th>
                <th>订单生成时间</th>
                <th>支付成功时间</th>
                <th>预发货时间</th>
                <th>发货时间</th>
                <th>订单状态</th>
                <th>支付方式</th>
                <th>地区</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            @foreach($orders as $key => $order)
                @if($order->warning == 1)
                    <tr class="tr_{{ $order->id }}" style="background:yellow;">
                @else
                    <tr class="tr_{{ $order->id }}">
                        @endif
                        <td>
                            <div class="order_img"><img src="{{ env('CDN_DOMAIN_NAME') }}/upload/{{ $order->face }}">
                            </div>
                            <ul class="order_ul">
                                <li class="order_li">品牌：{{ $brands[$order->brand_id] }}</li>
                                <li class="order_li">名称：{{ $order->title }}</li>
                                <li class="order_li">款号：{{ $order->girard }}</li>
                                <li class="order_li">颜色：{{ $order->color }}</li>
                                <li class="order_li">数量：{{ $order->num }}手</li>
                                <li class="order_li">
                                    金额：￥{{ sprintf("%.2f", $order->total_price +$order->postage_estimate) }}</li>
                            </ul>
                        </td>
                        <td><div style="width:100px;word-break:break-all">{{ $order->ordernum }}</div></td>
                        <td>
                            <p class="order_p">公司：{{ $company_name[$order->company_id] }}</p>
                            <p class="order_p">电话：{{$order->tel}}</p>
                            <p class="order_p">姓名：{{$order->addressee_name}}</p>
                        </td>
                        <td>{{$order->created_at}}</td>
                        <td class="success_time">{{$order->successed_at}}</td>
                        <td>{{ $order->estimate_send_at }}</td>
                        <td>{{$order->send_at}}</td>
                        <td class="td_status">

                            @if($order->status == 0)
                                订单生成
                            @elseif($order->status == 1)
                                待支付
                            @elseif($order->status == 2)
                                订单超时关闭
                            @elseif($order->status == 3)
                                @if($order->pay_type == 0)
                                    支付成功
                                @elseif($order->pay_type == 1)
                                    支付成功
                                @else
                                    支付成功
                                @endif
                            @elseif($order->status == 6)
                                支付成功
                            @elseif($order->status == 4)
                                订单二维码绑定完成
                            @elseif($order->status == 5)
                                确认收货
                            @elseif($order->status == -1)
                                已删除
                            @elseif($order->status == 7)
                                已发货
                            @elseif($order->status == -4)
                                内部付款
                            @elseif($order->status == -3)
                                已退款
                            @elseif($order->status == -2)
                                订单失效
                            @elseif($order->status == -9)
                                订单超时
                            @endif
                        </td>
                        <td>
                            @if(!in_array($order->status,[-1,0,-2,-9]))
                                @if($order->operation_id >0)
                                    线下支付
                                @else
                                    @if($order->pay_type === 0)
                                        微信支付
                                    @else
                                        支付宝支付
                                    @endif
                                @endif
                            @endif
                        </td>
                        <td>
                            @if($order->area_id > 0)
                                @if(isset($virtual_area[$order->area_id]))
                                    {{ $virtual_area[$order->area_id] }}
                                @endif
                            @endif
                        </td>
                        <td class="td_{{ $order->id }}" abc="{{ $order->id }}">
                            @if(in_array($order->status,[0,1]))
                                <button class="btn btn-success out_line" onclick="is_pay({{ $order->id }})">线下支付
                                </button>
                            @endif
                            @if(in_array($order->status,[0,1,2]))
                                <button class="btn delete" onclick="deleteOrder({{ $order->id }})"
                                        style="background-color:red;color:white;">删除
                                </button>
                            @elseif(in_array($order->status,[3,6,4,5,7]))
                                <button class="btn refund" style="background:#7266ba;color:white;">退款</button>
                            @elseif(in_array($order->status,[-1,-2,-9]))
                                不可操作
                            @endif
                            @if($order->status ==7)
                                <a href="/out-stage/expressDelivery?id={{ $order->id }}&page=0&see=1"
                                   class="btn btn-default look">查看</a>
                            @endif
                            @if(in_array($order->status,[0,1,2,-3]))
                                <button class="btn btn-info is_infor" data-toggle="modal" data-target="#myModal"><i
                                            class="fa fa-commenting"></i></button>
                            @endif
                            @if(in_array($order->status,[3,6,-3]))
                                <button class="btn edit" style="background:#c2ce89;color:#fff;">编辑</button>
                            @endif
                        </td>
                    </tr>
                    @endforeach
            </tbody>
        </table>
        <div style="float: right">
            <div style="float: right;margin:20px 0;">
                <button class="btn btn-info" onclick="returnPage()" style="float:right;">跳转</button>
                <input type="text" class="form-control returnPage" style="width:6rem;float: right;margin:0 5px;">
            </div>{{$orders->appends(['company_id'=>$company_id,'start_at'=>$start_at,'pre_sale'=>$pre_sale,'end_at'=>$end_at,'user_tel'=>$tel,'user_name'=>$user_name,'girard'=>$girard,'status'=>$status_id])->links()}}
        </div>

        <!-- 模态框（Modal） -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
             aria-hidden="true">
            <div class="modal-dialog" style="width:950px;height:535px;overflow-y:auto;">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">信息</h4>
                    </div>
                    <div class="modal-body">
                        <div class="container" style="width:100%;">
                            <div class="row">
                                <table class="table table-hover pass_name">
                                    <thead>
                                    <tr>
                                        <th>用户名</th>
                                        <th>手机号</th>
                                        <th>地址</th>
                                        <th>款号</th>
                                        <th>颜色</th>
                                        <th>数量</th>
                                        <th>点击时间</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody class="info_main">

                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        /*日期插件初始化*/
        $("#date1").dateSelect();
        $("#date2").dateSelect();
        $(".td_status").each(function () {
            if ($(this).text().trim() == "已删除") {
                $(this).css("color", "red");
            }
        });
        /*退款*/
        $(document).on("click", ".refund", function () {
            var id = $(this).parent().attr("abc");
            $(".oText").text("确认要退款吗？");
            $(".oMask").show();
            $(".oAnim").show();
            $(".oBtn0").attr("abc", id);
            $(".oBtn0").attr('id', '');
            $(".oBtn0").attr('id', 'oRefund');
        });
        /*点确定*/
        $(document).on('click', '#oRefund', function () {
            $(".oMask").hide();
            $(".oAnim").hide();
            var id = $(this).attr("abc");
            $.ajax({
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                url: '/order/orderRefund',
                data: {'id': id},
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        $('.td_' + id).children(".refund").remove();
                        $('.td_' + id).children(".look").remove();
                        $('.td_' + id).children(".edit").remove();
                        $('.td_' + id).append("<button class='btn btn-info is_infor' data-toggle='modal' data-target='#myModal'><i class='fa fa-commenting'></i></button>")
                        $('.td_' + id).parent().children(".td_status").text("已退款");
                        $(".faceImg").attr("src", "/img/laugh.png");
                        $(".oHandle").text("退款成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    } else {
                        $(".faceImg").attr("src", "/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        });

        /*删除*/
        function deleteOrder(id) {
            $(".oText").text("确定要删除吗？");
            $(".oMask").show();
            $(".oAnim").show();
            $(".oBtn0").attr("abc", id);
            $(".oBtn0").attr('id', '');
            $(".oBtn0").attr('id', 'oDelete');
        }

        /*点确定*/
        $(document).on('click', '#oDelete', function () {
            $(".oMask").hide();
            $(".oAnim").hide();
            var id = $(this).attr("abc");
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                url: '/order/delete_order',
                data: {
                    'id': id
                },
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        $('.tr_' + id).children(".td_status").empty();
                        $('.tr_' + id).children(".td_status").css("color", "red");
                        $('.tr_' + id).children(".td_status").text("已删除");

                        $('.td_' + id).empty();
                        $('.td_' + id).text("不可操作");
                        $(".faceImg").attr("src", "/img/laugh.png");
                        $(".oHandle").text("删除成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    } else {
                        $(".faceImg").attr("src", "/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        });

        //线下支付成功
        function is_pay(id) {
            $(".oText").text("确认线下已支付？");
            $(".oMask").show();
            $(".oAnim").show();
            $(".oBtn0").attr("abc", id);
            $(".oBtn0").attr('id', '');
            $(".oBtn0").attr('id', 'oPay');
        }

        /*点确定*/
        $(document).on('click', '#oPay', function () {
            $(".oMask").hide();
            $(".oAnim").hide();
            var id = $(this).attr("abc");
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                url: '/company/setOrderStatus',
                data: {
                    'order_id': id
                },
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        $('.tr_' + id).children(".success_time").text(data.info);
                        $('.tr_' + id).children(".td_status").text("线下支付成功");
                        $('.td_' + id).empty();
                        $('.td_' + id).prepend("<button class='btn edit' style='background:#c2ce89;color:#fff;'>编辑</button>");
                        $('.td_' + id).prepend("<button class='btn refund' style='background:#7266ba;color:white;'>退款</button>");
                        $(".faceImg").attr("src", "/img/laugh.png");
                        $(".oHandle").text("线下支付成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    } else {
                        $(".faceImg").attr("src", "/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        });

        /*导出*/
        function Export() {
            var company_id = $(".com_id").val();
            var status = $(".statu_id").val();
            var user_tel = $(".tel_id").val();
            var girard = $(".style_id").val();
            var start_at = $(".start_time").val();
            var end_at = $('.end_time').val();
            var user_name = $('.user_name').val();

            //alert(user_name);
            window.location.href = "/excel/export?company_id=" + company_id + "&status="+status + "&user_name=" + user_name + "&user_tel=" + user_tel + "&girard=" + girard + "&start_at=" + start_at + "&end_at=" + end_at;
        }

        /*信息*/
        $(document).on("click", ".is_infor", function () {
            var id = $(this).parent().attr("abc");
            $(".pass_name .info_main").empty();
            $.ajax({
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                url: '/order/getPurchaseIntetionUser',
                data: {'id': id},
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    if (data.success) {
                        var info = data.data;
                        //console.log(info);
                        for (var i in info) {
                            if (info[i].status == 1) {
                                var str = "<tr>" +
                                    "<td>" + info[i].user_name + "</td>" +
                                    "<td>" + info[i].user_tel + "</td>" +
                                    "<td>" + info[i].place + "</td>" +
                                    "<td>" + info[i].girard + "</td>" +
                                    "<td>" + info[i].color_name + "</td>" +
                                    "<td>" + info[i].num + "</td>" +
                                    "<td>" + info[i].created_at + "</td>" +
                                    "<td><i class='fa fa-check'></i></td>" +
                                    "</tr>";
                            } else {
                                var str = "<tr>" +
                                    "<td>" + info[i].user_name + "</td>" +
                                    "<td>" + info[i].user_tel + "</td>" +
                                    "<td>" + info[i].place + "</td>" +
                                    "<td>" + info[i].girard + "</td>" +
                                    "<td>" + info[i].color_name + "</td>" +
                                    "<td>" + info[i].num + "</td>" +
                                    "<td>" + info[i].created_at + "</td>" +
                                    "<td class='success_in pass_" + info[i].id + "'><button class='btn btn-info user_people' abc=" + info[i].id + ">询问</button></td>" +
                                    "</tr>";
                            }
                            $(".pass_name .info_main").append(str);
                        }
                    } else {
                        $(".faceImg").attr("src", "/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        })
        /*询问*/
        $(document).on("click", ".user_people", function () {
            var that = $(this);
            var id = $(this).attr("abc");
            $(".oText").text("确认已询问？");
            $(".oMask").show();
            $(".oAnim").show();
            $(".oBtn0").attr("abc", id);
            $(".oBtn0").attr('id', '');
            $(".oBtn0").attr('id', 'oAsk');
            /*点确定*/
            $(document).on('click', '#oAsk', function () {
                $(".oMask").hide();
                $(".oAnim").hide();
                var id = $(this).attr("abc");
                $.ajax({
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                    url: '/order/setPurchaseIntentionStatus',
                    data: {'id': id},
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        if (data.success) {
                            that.parent(".success_in").append("<i class='fa fa-check'></i>");
                            that.remove();
                            $(".faceImg").attr("src", "/img/laugh.png");
                            $(".oHandle").text("操作成功");
                            $(".oSuccess").show(0).delay(1000).hide(0);
                        } else {
                            $(".faceImg").attr("src", "/img/weep.png");
                            $(".oHandle").text(data.msg);
                            $(".oSuccess").show(0).delay(1000).hide(0);
                        }
                    }
                })
            });
        });
                @if(isset($_GET['page']))
        var page = '{{ $_GET['page'] }}';
                @else
        var page = 1;
        @endif
        /*编辑*/
        $(document).on("click", ".edit", function () {
            var id = $(this).parent().attr("abc")
            window.location.href = "/order/getOrderDetails?id=" + id + "&page=" + page;
        })
    </script>
@endsection